package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.ResponseUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/topic/batch-delete")
public class AdminTopicBatchDelete extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        Map idsMap = JacksonUtil.read(jsonStr, Map.class);
        List idsList = (List) idsMap.get("ids");
        String idsStr = idsList.toString();
        String[] strings = idsStr.substring(1, idsStr.length() - 1).split(", ");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketTopicMapper mapper = sqlSession.getMapper(MarketTopicMapper.class);

        for (String string : strings) {
            int i = Integer.parseInt(string);

            MarketTopic marketTopic = mapper.selectByPrimaryKey(i);
            System.out.println("i======" + i);

            System.out.println(marketTopic.toString());
            marketTopic.setDeleted(true);
            mapper.updateByPrimaryKey(marketTopic);
        }
        sqlSession.commit();
        sqlSession.close();
        ResponseUtil.responseOkJsonVo(response, null);
    }
}